package nc.tb.pub.table;

import javax.swing.table.TableColumnModel;

import nc.ui.pub.beans.UITable;
import nc.ui.pub.beans.UITablePane;

public class TableColumnResizer {
	
	public static void fitColumnSize(UITablePane panel, boolean isFirstCheckCol) {
		fitColumnSize(panel, isFirstCheckCol, false, 70, 0);
	}
	public static void fitColumnSize(UITable table, boolean isFirstCheckCol) {
		fitColumnSize(table, isFirstCheckCol, false, 70, 0);
	}
	public static void fitColumnSize(UITablePane panel, boolean isFirstCheckCol, boolean isOnlyFitColHead, int checkColWidth, int allWidth) {
		fitColumnSize(panel.getTable(), isFirstCheckCol, isOnlyFitColHead, checkColWidth, allWidth);
	}
	public static void fitColumnSize(UITable table, boolean isFirstCheckCol, boolean isOnlyFitColHead, int checkColWidth, int allWidth) {
//		UITable table = panel.getTable();
		TableColumnModel colmodel = table.getColumnModel();
		int rowcount = table.getRowCount();
		int column = table.getColumnCount();
		java.awt.FontMetrics fontm = table.getFontMetrics(table.getFont());
		int beginIdx = 0;
		int totalWidth = 0;
		int[] colWidth = new int[column];
		if (isFirstCheckCol) {
			colmodel.getColumn(0).setWidth(checkColWidth);
			colWidth[0] = checkColWidth;
			totalWidth += colWidth[0];
			table.sizeColumnsToFit(0);
			beginIdx++;
		}
		for (int i = beginIdx; i < column; i++) {
			String head = colmodel.getColumn(i).getHeaderValue().toString();
			int width = fontm.stringWidth(head);
			for (int j = 0; j < rowcount; j++) {
				Object obj = table.getValueAt(j, i);
				if (obj != null)
					width = Math.max(width, fontm.stringWidth(obj.toString()));
			}
			colmodel.getColumn(i).setWidth(width + 20);
			colWidth[i] = width + 20;
			totalWidth += colWidth[0];
			table.sizeColumnsToFit(i);
		}
		isFirstCheckCol = false;
		if (totalWidth < allWidth) {
			allWidth = isFirstCheckCol ? allWidth - colWidth[0] : allWidth;
			totalWidth = isFirstCheckCol ? totalWidth - colWidth[0] : totalWidth;
			int idx = isFirstCheckCol ? 1 : 0;
			for (int i=idx; i<column; i++) {
				int width = allWidth / totalWidth * colWidth[i];
				colmodel.getColumn(i).setWidth(width);
				table.sizeColumnsToFit(i);
			}
		}
	}
	
	public static void fitColumnSizeOnlyByColumn(UITablePane panel, boolean isFirstCheckCol) {
		fitColumnSize(panel, isFirstCheckCol, true, 70, 0);
//		UITable table = panel.getTable();
//		TableColumnModel colmodel = table.getColumnModel();
////		int rowcount = table.getRowCount();
//		int column = table.getColumnCount();
//		java.awt.FontMetrics fontm = table.getFontMetrics(table.getFont());
//		int beginIdx = 0;
//		if (isFirstCheckCol) {
//			colmodel.getColumn(0).setWidth(70);
////			table.sizeColumnsToFit(0);
//			beginIdx++;
//		}
//		for (int i = beginIdx; i < column; i++) {
//			String head = colmodel.getColumn(i).getHeaderValue().toString();
//			int width = fontm.stringWidth(head);
////			for (int j = 0; j < rowcount; j++) {
////				Object obj = table.getValueAt(j, i);
////				if (obj != null)
////					width = Math.max(width, fontm.stringWidth(obj.toString()));
////			}
//			colmodel.getColumn(i).setWidth(width + 20);
//			table.sizeColumnsToFit(i);
//		}
	}
	
	public static void fitColumnSize(UITablePane panel, int[] colWidth) {
		UITable table = panel.getTable();
		TableColumnModel colmodel = table.getColumnModel();
		int rowcount = table.getRowCount();
		java.awt.FontMetrics fontm = table.getFontMetrics(table.getFont());
		int column = table.getColumnCount();
		if (colWidth.length != column)
			return;
		for (int i=0; i<column; i++) {
			if (colWidth[i] >= 0)
				colmodel.getColumn(i).setWidth(colWidth[i]);
			else {
				String head = colmodel.getColumn(i).getHeaderValue().toString();
				int width = fontm.stringWidth(head);
				for (int j = 0; j < rowcount; j++) {
					Object obj = table.getValueAt(j, i);
					if (obj != null)
						width = Math.max(width, fontm.stringWidth(obj.toString()));
				}
				colmodel.getColumn(i).setWidth(width + 20);
			}
			table.sizeColumnsToFit(i);
		}
	}

}
